package com.gamerole.plugin.mvvmhttp;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.project.Project;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        Editor mEditor = e.getData(PlatformDataKeys.EDITOR);
        final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
        if (null == mEditor) {
            return;
        }
        String substring = ((EditorImpl) mEditor).getVirtualFile().getPath().substring(project.getBasePath().length() + 1);
        FileUtil.projectModelName = substring.substring(0, substring.indexOf("/"));
        FileUtil.projectPackge = dealPath(project.getBasePath() + "/" + FileUtil.projectModelName + "/src/main/AndroidManifest.xml");
        FileUtil.projectFilePath = project.getBasePath() + "/" + FileUtil.projectModelName + "/src/main/java/" + FileUtil.projectPackge.replace(".", "/");
        SelectionModel model = mEditor.getSelectionModel();
        String selectedText = model.getSelectedText();
        FileUtil.presenterFilePath = selectedText;

        /**
         * 文件名的前缀
         */

        System.out.println(project.getBasePath());
        TestDialog sampleDialogWrapper = new TestDialog(project, project.getBasePath() + "/auto.json");
        sampleDialogWrapper.setVisible(true);
//        Messages.showMessageDialog(project,FileUtil.projectPackge, FileUtil.projectFilePath, Messages.getInformationIcon());
    }

    public String dealPath(String filePath) {
        String fileContent = FileUtil.getFileContent(filePath);
        String pattern = "package=\"([\\s\\S].*?)\"";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern, Pattern.UNIX_LINES);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(fileContent);


        m.find();

        return m.group(1);

    }
}
